import Vue         from 'vue'
import Router      from 'vue-router'
import {post}      from '@/util/request'
import iView       from 'iview'

import Login       from '@/components/Login'
import Index       from '@/components/Index'
import Home        from '@/components/Home'
import HistoryPath from '@/components/HistoryPath'

// 运营管理
import OperLogisticsCompany   from '@/components/operation/LogisticsCompany'
import OperSeller             from '@/components/operation/Seller'
import OperStatementAccount   from '@/components/operation/StatementAccount'
import OperDriver             from '@/components/operation/Driver'
import OperDriverDetail       from '@/components/operation/DriverDetail'
import OperStatisticsSummary  from '@/components/operation/StatisticsSummary'
import OperStatisticsOnline   from '@/components/operation/StatisticsOnline'
import OperStatisticsNetwork  from '@/components/operation/StatisticsNetwork'
import Spitslot               from '@/components/operation/Spitslot'

// 基础信息管理
import BaseinfoDriver         from '@/components/baseinfo/Driver'
import BaseinfoVehicle        from '@/components/baseinfo/Vehicle'

// 车队视图
import FleetView        from '@/components/fleet/FleetView'
import FleetStatistics  from '@/components/fleet/FleetStatistics'

// 统计汇总
import RegionalSafety   from '@/components/statistics/RegionalSafety'
import FleetSummary  from '@/components/statistics/FleetSummary'

// 违章视图
import ViolationView  from '@/components/violation/View'
import ViolationList  from '@/components/violation/List'

// 安全排名
import RankGroup      from '@/components/rank/Group'
import RankInternal   from '@/components/rank/Internal'

// 司机视图
import DriverView     from '@/components/driver/View'
import DriverDetail   from '@/components/driver/Detail'
import DriverStatistics   from '@/components/driver/DriverStatistics'



//系统管理
import Employee       from '@/components/system/Employee'
import Job            from '@/components/system/Job'
import Organization   from '@/components/system/Organization'

// 排班管理
import Sche           from '@/components/sche/Sche'

// 预警记录
import AlarmInfo      from '@/components/alarm/AlarmInfo'

// 公告/提醒
import Announcement   from '@/components/announcement/Announcement'
// 事故
import AccidentList   from '@/components/accident/AccidentList'
import AccidentStat   from '@/components/accident/AccidentStat'


Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Index',
      component: Index,
      redirect:'/home',
      children:[
        {
          path: '/home',
          name: 'home',
          component: Home
        },
        {
          path: '/history',
          name:'history',
          component:HistoryPath
        },

        // 车队视图
        {
          path: '/fleet/view',
          name: 'fleetView',
          component: FleetView
        },
        {
          path: '/fleet/statistics',
          name: 'fleetStatistics',
          component: FleetStatistics
        },

        // 统计汇总
        {
          path: '/statistics/regionalSafety',
          name: 'regionalSafety',
          component: RegionalSafety
        },
        {
          path: '/statistics/fleetSummary',
          name: 'fleetSummary',
          component: FleetSummary
        },

        // 违章视图
        {
          path: '/violation/view',
          name: 'violationView',
          component: ViolationView
        },
        {
          path: '/violation/list',
          name: 'violationList',
          component: ViolationList
        },

        // 安全排名
        {
          path: '/rank/group',
          name: 'rankGroup',
          component: RankGroup
        },
        {
          path: '/rank/internal',
          name: 'rankInternal',
          component: RankInternal
        },

        // 司机视图
        {
          path: '/driver/view',
          name: 'driverView',
          component: DriverView
        },
        {
          path: '/driver/view/detail',
          name: 'driverDetail',
          component: DriverDetail
        },
        {
          path: '/driver/driverStatistics',
          name: 'driverStatistics',
          component: DriverStatistics
        },

        // 基础信息管理
        {
          path: '/baseinfo/driver',
          name: 'baseinfoDriver',
          component: BaseinfoDriver
        },
        {
          path: '/baseinfo/vehicle',
          name: 'baseinfoVehicle',
          component: BaseinfoVehicle
        },

        //系统管理
        {
          path: '/system/employee',
          name: 'employee',
          component: Employee
        },
        {
          path: '/system/job',
          name: 'job',
          component: Job
        },
        {
          path: '/system/organization',
          name: 'organization',
          component: Organization
        },

        // 排班管理
        {
          path: '/sche/sche',
          name: 'sche',
          component: Sche
        },
        // 预警记录
        {
          path: '/alarm/alarmInfo',
          name: 'alarmInfo',
          component: AlarmInfo
        },
        // 公告/提醒
        {
          path: '/announcement/announcement',
          name: 'announcement',
          component: Announcement
        },
        // 事故
        {
          path: '/accident/list',
          name: 'accidentList',
          component: AccidentList
        },
        {
          path: '/accident/stat',
          name: 'accidentStat',
          component: AccidentStat
        },

        // 运营管理
        {
          path: '/operation/logisticsCompany',
          name: 'operLogisticsCompany',
          component: OperLogisticsCompany
        },
        {
          path: '/operation/seller',
          name: 'operSeller',
          component: OperSeller
        },
        {
          path: '/operation/statementAccount',
          name: 'operStatementAccount',
          component: OperStatementAccount
        },
        {
          path: '/operation/driver',
          name: 'operDriver',
          component: OperDriver
        },
        {
          path: '/operation/driver/detail',
          name: 'operDriverDetail',
          component: OperDriverDetail
        },
        {
          path: '/operation/statisticsSummary',
          name: 'operStatisticsSummary',
          component: OperStatisticsSummary
        },
        {
          path: '/operation/statisticsOnline',
          name: 'operStatisticsOnline',
          component: OperStatisticsOnline
        },
        {
          path: '/operation/spitslot',
          name: 'spitslot',
          component: Spitslot
        },
        {
          path: '/operation/statisticsNetwork',
          name: 'operStatisticsNetwork',
          component: OperStatisticsNetwork
        },
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    }
  ]
})


const bindStore = store => {
  // router.beforeEach(async (to,from,next)=>{
  //   iView.LoadingBar.start();
  //   console.log(to.path);
  //   if(to.path !== '/login'){
  //     let a_path = to.path.indexOf('&gid')>-1?to.path.split('&gid')[0]:to.path;
  //     let res = await post('/isLogin',{resUrl:a_path});
  //     let path = to.path;
  //     if(to.path.indexOf('&gid')>-1){
  //       path = to.path.split('&gid')[0];
  //
  //       if(to.fullPath.indexOf('?')>-1){
  //         path += '?'+to.fullPath.split('?')[1];
  //       }
  //       if(res.data){
  //         res.data.sta === 200?next(path):next('/login');
  //       }
  //     }else{
  //        if(res.data){
  //         res.data.sta === 200?next():next('/login');
  //       }
  //     }
  //   }else{
  //     next();
  //   }
  // });

  router.afterEach(route => {
    iView.LoadingBar.finish();
  });

  for(let key in store._modulesNamespaceMap){
    const m = store._modulesNamespaceMap[key];
    m._rawModule.subscriptions&&m._rawModule.subscriptions(m.context,router);
  }


}



export default router;
export {bindStore}
